const app = getApp();

const _http =(method, urlTail, data, response, error, retryCount) =>{
    const reqData = {body:data}
    let loginToken = '';
    if(app.globalData.userInfo){
        loginToken = app.globalData.userInfo.loginToken
    }
    const url = app.globalData.httpDomain + urlTail;
    console.log("url = " +url+ " authorization = " + loginToken );
    console.log(reqData);
    wx.request({
        method: method,
        url: url,
        data: reqData,
        header: {
            'content-type': 'application/json',
            'authorization': loginToken,
        },
        success: res =>{
            console.log(res)
            if(res.data.code == 198 && retryCount < 3){
                // 登录
                _login(()=>{
                    retryCount = retryCount + 1;
                   _http(method, urlTail, data, response, error, retryCount)
                },(res)=>{
                  if(error){return error(res)}
                })
            }else{
                return response(res)
            }
        },
        fail:err =>{
            if(error){
                return error(err)
            }
        },
        complete:info =>{}
    });
}

const _login =(response, error) => {
    wx.login({
        success: res => {
            var url = app.globalData.httpDomain + _url.wxLogin;
            var reqData = {
                code: res.code,
                appId: wx.getAccountInfoSync().miniProgram.appId
            }
            console.log(url);
            console.log(reqData);
            wx.request({
                method: 'POST',
                url:url,
                data: {
                    body:reqData
                },
                success: res =>{
                    console.log(res)
                    if(res.data.code == 200){
                        app.globalData.userInfo = res.data.body;
                        return response(res)
                    }else{
                        if(error){
                            return error(res)
                        }
                    }
                },
                fail:err =>{
                    console.log(err);
                    if(error){
                        return error(err)
                    }
                },
            })
        },
        fail: res => {
            console.log(res);
        }
    });
}

const _url = {
    /* 登录注册 */
    wxLogin:'/xcx/v1/wx/login/check.do'

    /* 商品 */
    ,findGoodsList:'/market/v1/goods/list.data'      //获取商品列表接口
    ,getGoodsInfo:'/market/v1/goods/info.data'       //获取商品详情接口

    ,goodsPayCheck:'/market/v1/goods/pay/check.do'
    ,goodsPay:'/market/v1/goods/pay.do'

    ,findMarketOrderList:'/market/v1/member/order/list.data'
    ,findOrderCount:'/market/v1/member/order/count.data'
    ,getMarketOrder:'/market/v1/member/order/info.data'
    ,closeMarketOrder:'/market/v1/member/order/close.do'
    ,receiveMarketOrder:'/market/v1/member/order/goods/receive.do'

    /* 会员收货地址 */
    ,findAddressList:'/market/v1/member/address/list.data'        //获取会员送货地址列表接口
    ,saveAddress:'/market/v1/member/address/save.do'              //新增会员送货地址接口
    ,getAddressInfo:'/market/v1/member/address/info.data'     //获取会员详细地址信息
    ,delAddress:'/market/v1/member/address/del.do'         //删除会员送货地址接口
}

module.exports = {
  url:_url,
  //请求方式、请求地址、请求参数、回调函数、错误处理
  get:(url,data,response,error) => _http('GET',url,data,response,error,0),
  post:(url,data,response,error) => _http('POST',url,data,response,error,0)
}